# method can be "ADX", "MARUBOZU"
StockStatus <- function(TS, tblname="dummy", insertIntoDB=FALSE, subsetstring="", Oppn=5, method="") {
  if (method=="") stop ("parameter method must be given")
  ADXn <- 14        # n for ADX Calculation  
  SMAn1 <- 200      # for SMA Calculation
  SMAn2 <- 50
  SMAn3 <- 10
  bgslash <- max(c(SMAn1, SMAn2, SMAn3, (ADXn*2+1))) # slash the first rows that contain NA values
  edslash <- Oppn
  # ADX Status
  if (method=="ADX") {
    ADXS <- ADXStatus(TS, n=ADXn, addSig=TRUE)
  } else {
    ADXS <- ADXStatus(TS, n=ADXn, addSig=FALSE)    
  }
  # SMA Status
  SMAS1 <- SMAStatus(TS, n=SMAn1)
  SMAS2 <- SMAStatus(TS, n=SMAn2)
  SMAS3 <- SMAStatus(TS, n=SMAn3)  

  # Opportunities
  c0OP <- findOpportunity(TS, n=Oppn, method="c0")
  o1OP <- findOpportunity(TS, n=Oppn, method="o1")
  c1OP <- findOpportunity(TS, n=Oppn, method="c1")
  OP <- cbind(c0OP[,"buyS"], c0OP[,"sellS"], o1OP[,"buyS"], o1OP[,"sellS"], c1OP[,"buyS"], c1OP[,"sellS"])
  colnames(OP) <- c("c0_buyS", "c0_sellS", "o1_buyS", "o1_sellS", "c1_buyS", "c1_sellS")
  if (method=="ADX") {
    result <- cbind(ADXS, SMAS1, SMAS2, SMAS3, OP)
  } else if (method=="MARUBOZU") {
    CSPS <- CSPStatus(TS, c("MARUBOZU"))
    result <- cbind(ADXS, SMAS1, SMAS2, SMAS3, CSPS, OP)
  } else if (method=="MARUBOZU_1_3") {
    CSPS <- CSPStatus(TS, c("MARUBOZU_1_3"))
    result <- cbind(ADXS, SMAS1, SMAS2, SMAS3, CSPS, OP)
  } else if (method=="GAP") {
    CSPS <- CSPStatus(TS, c("GAP"))
    result <- cbind(ADXS, SMAS1, SMAS2, SMAS3, CSPS, OP)    
  } else if (method=="STOMACH") {
    CSPS <- CSPStatus(TS, c("STOMACH"))
    result <- cbind(ADXS, SMAS1, SMAS2, SMAS3, CSPS, OP)    
  } else if (method=="THREE") {
    CSPS <- CSPStatus(TS, c("THREE"))
    result <- cbind(ADXS, SMAS1, SMAS2, SMAS3, CSPS, OP)    
  } else if (method=="DONCHIAN") {
    DCS <- DCStatus(TS, n=10)
    result <- cbind(ADXS, SMAS1, SMAS2, SMAS3, DCS, OP)    
  }
  dres <- dim(result)
  # now remove rows that contain NA entries
  result <- result[bgslash:(dres[1]-edslash),]
  # now apply subsetstring to fit xts in group-defined date boundaries
  result <- result[subsetstring]
  if (insertIntoDB==TRUE) {
    cnames <- colnames(result)
    con <- connectToVadb()
    createVadbTable(con, tblname, cnames[1:(length(cnames)-6)], cnames[(length(cnames)-5):length(cnames)])
    fillVadbTable(con, tblname, "undefined", result)
    disconnectFromVadb(con)
  } else {
    return(result)    
  }
}

# method can be "c0", "o1", "c1"
groupStockStatus <- function(groupcode, tblname="SS", Oppn=5, verbose=TRUE, method="") {
  if (method=="") stop ("parameter method must be given")
  tblname <- paste(tblname, method, sep="_")
  if (verbose==TRUE) cat("start groupStockStatus, group", groupcode,", method", method, ":", format(Sys.time(), "%Y-%m-%d %X"), "\n")
  getSymbolsGroup(groupcode, daysbefore=350, daysafter=30)
  groupinfo <- getDBGroup(groupcode)
  dimgroupinfo <- dim(groupinfo)
  con <- connectToVadb()
  
  for (i in 1:dimgroupinfo[1]) {
    SS <- StockStatus(get(paste(groupinfo[i,"code"], ".vadb", sep="")), 
                insertIntoDB=FALSE,
                subsetstring=paste(groupinfo[i,"from"], groupinfo[i,"to"], sep="/"),
                Oppn=Oppn,
                method=method)
    if(i==1) {  # at first iteration create the table
      ftblname <- paste(tblname, groupcode, sep="_")
      cnames <- colnames(SS)
      if (ftblname %in% dbListTables(con)) {
        stop ("given table name in database already exists")
      } else {
        createVadbTable(con, ftblname, 
                        cnames[1:(length(cnames)-6)], cnames[(length(cnames)-5):length(cnames)])
      }
    }
    fillVadbTable(con, ftblname, groupinfo[i,"code"], SS)
  }
  if (verbose==TRUE) cat("stop groupStockStatus : ", format(Sys.time(), "%Y-%m-%d %X"), "\n")
}
